import { Util } from '../../utils/util.js';
import { OrgSettings } from './orgSettings.js';
import { MerchantRetailSettings } from '../retail/merchantRetailSettings.js';
import { MerchantActivePackage } from './merchantActivePackage.js';
import { Store } from '../store/store.js';
import { TargetObject } from '../targetOrg/targetObject.js';
import { PermissionInfo } from '../menus/permissionInfo.js';

export class MerchantOrg {
    constructor() {
        // 不初始化任何字段，留给静态构造方法
    }

    static empty(orgId = '') {
        const instance = new MerchantOrg();
        instance.orgId = orgId;
        instance.contactPerson = '';
        instance.createTime = new Date();
        instance.createUser = '';
        instance.owner = '';
        instance.addr = '';
        instance.logoUri = '';
        instance.name = '';
        instance.parentOrgId = '';
        instance._orgSettings = new OrgSettings();
        instance._retailSettings = new MerchantRetailSettings();
        instance.activePkg = null;
        instance.publicStore = null;
        instance.defaultCustomer = null;
        instance.defaultSupplier = null;
        instance._permissionInfo = new PermissionInfo();
        instance._customerFundAccountTypeList = null;  // 客户资金账户类型列表（用于贵金属等）
        return instance;
    }


    get customerFundAccountTypeList() {
        return this._customerFundAccountTypeList;
    }

    set customerFundAccountTypeList(value) {
        this._customerFundAccountTypeList = value;
    }

    get orgPermissionInfo() {
        return this._permissionInfo;
    }

    set orgPermissionInfo(value) {
        this._permissionInfo = value;
    }

    get currencyCharector() {
        return '¥';
    }

    get printCurrencyCharector() {
        return '￥';
    }

    get settings() {
        return this._orgSettings;
    }

    get retailSettings() {
        return this._retailSettings;
    }

    // 是否贵金属商户
    get isPreciousMetalOrg() {
        if (this._isPreciousMetalOrg) {
            return this._isPreciousMetalOrg;
        }
        return false;
    }

    updateSettings(settings) {
        this._orgSettings = settings;
    }

    updateRetailSettings(settings) {
        this._retailSettings = settings;
    }

    toJson() {
        return {
            orgId: this.orgId,
            orgName: this.name
        };
    }

    static fromJson(json) {
        const instance = new MerchantOrg();
        instance.orgId = Util.jsonToString(json.orgId);
        instance.contactPerson = Util.jsonToString(json.contactPerson);
        instance.createTime = json.createTime ? new Date(json.createTime) : new Date();
        instance.createUser = Util.jsonToString(json.createUser);
        instance.owner = Util.jsonToString(json.followUserId);
        instance.addr = Util.jsonToString(json.addr);
        instance.logoUri = Util.jsonToString(json.orgLogo);
        instance.name = Util.jsonToString(json.orgName);
        instance.parentOrgId = Util.jsonToString(json.parentId);
        instance._orgSettings = new OrgSettings();
        instance._retailSettings = new MerchantRetailSettings();
        instance.activePkg = null;
        instance.publicStore = null;
        instance.defaultCustomer = null;
        instance.defaultSupplier = null;
        instance._permissionInfo = new PermissionInfo();
        instance._isPreciousMetalOrg = Util.jsonToBool(json.isPreciousMetalOrg);  // 是否贵金属商户
        return instance;
    }
}
